<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Code by TinhNQ
 * 2014
 **/
class Citys_model extends MY_Model {

    protected $city;

    function __construct(){
        parent::__construct();
    }

    function table_name(){
        return "citys";
    }

    function get_citys($filters = array(),$counting = false){
        if (isset($this->CI->cache)){
            $caching = true;
            $cache_key = 'ci--City_'.md5(serialize($filters));
            if ($counting){
                $cache_key = 'ci--City_COUNT_'.md5(serialize($filters));
            }
            if ($return = $this->cache->file->get($cache_key)) {
                return ($return == 'empty_cache') ? FALSE : $return;
            }
        }else{
            $caching = false;
        }

        $this->db->select('citys.*, prefs.name as pref_name');
        $this->db->from('citys');
        $this->db->join('prefs', 'prefs.pref_id = citys.pref_id');
        $param = array();

        $this->db->order_by('citys.city_id','DESC');

        if (isset($filters['city_id'])) {
            $this->db->where('citys.city_id', $filters['city_id']);
        }
        if (isset($filters['name'])) {
            $this->db->like('citys.name', $filters['name']);
        }
        if (isset($filters['alias'])) {
            $this->db->where('citys.alias', $filters['alias']);
        }
        if (isset($filters['pref_id'])) {
            $this->db->where('citys.pref_id', $filters['pref_id']);
        }
        if (isset($filters['status'])) {
            $this->db->where('citys.status', $filters['status']);
        }

        if (isset($filters['limit'])) {
            $offset = (isset($filters['page'])) ? $filters['page'] : 0;
            $this->db->limit($filters['limit'], $offset);
        }

        $result = $this->db->get();

        if($counting !=false){
            $rows = $result->num_rows();
            $result->free_result();

            if ($caching == TRUE) {
                $this->CI->cache->file->save($cache_key, $rows, (5*60));
            }

            return $rows;
        }
        if ($result->num_rows() == 0) {
            return false;
        }
        $city = array();
        foreach ($result->result_array() as $row ){
            $row['link'] = create_url('city/'.$row['alias'],array('rewrite'=>true));
            //$row['date'] = fdate($row['created'],"%A, %d/%m/%Y - %H:%M");
            $city[] = $row;
        }
        if ($caching){
            $this->CI->cache->file->save($cache_key,$city);
        }
        $result->free_result();

        return $city;
    }

    function get_count($filter = array()){
        return $this->get_citys($filter,true);
    }

    function get_city($cityId){
        if (!isset($this->city[$cityId])){
            $city = $this->get_citys(array('city_id'=>$cityId));
            $this->city[$cityId] = $city;
        }else{
            $city = $this->city[$cityId];
        }
        if (empty($city)) {
            return false;
        }
        return $city[0];
    }

    function insert_city($data){
        $_city_id = $this->save($data);
        return $_city_id;
    }

    function update_city($data, $city_id){
        return $this->update($data, array('city_id' => $city_id));
    }

    function delete_city($city_id) {
        return $this->delete(array('city_id'=>$city_id));
    }

    function change_status_city($city_id) {
        $status = $this->db->select('status')->from('citys')->where('city_id', $city_id)->get()->result_array();

        if ($status[0]['status'] == 1) {
            $change = 0;
        } else {
            $change = 1;
        }
        return $this->update(array('status' => $change), array('city_id' => $city_id));
    }

    function get_city_by_alias($city_alias){
        $city = $this->get_citys(array('alias'=>$city_alias),false);
        if ($city){
            return $city[0];
        }
        return false;
    }

    function get_citys_options($filters=array()){
        $citys_options = array();
        $citys = $this->get_citys($filters);
        if (is_array($citys) && count($citys)){
            foreach ($citys as $city){
                $citys_options[$city['city_id']] = $city['name'];
            }
        }
        return $citys_options;
    }
}
?>